﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class RecommendedUserNumberEditControl : YuanlinUserControl,
        IReplyConverter<RecommendedUserNumberReply, GetOperation>
    {
        protected ValuedTextBox numberValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private RecommendedUserNumber recommendedUserNumber;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetRecommendedUserNumberRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Attributes["onclick"] = "return CheckValue()";
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if(recommendedUserNumber!=null)
            {
                this.numberValuedTextBox.Value = recommendedUserNumber.Number.Value;
            }   
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetRecommendedUserNumberListUrl());
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
            request.GreenNum = this.numberValuedTextBox.Value;
            EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
            if (companyGreenNums.Count > 0)
            {
                Globals.ShowMessage(this.Page, "ExistCompanyGreenNum", "此号码已经被注册过！");
                return;
            }

            recommendedUserNumber.Number.Value = this.numberValuedTextBox.Value;

            if (BusinessManager.UpdateRecommendedUserNumber(recommendedUserNumber) == -2)
            {
                Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                return;
            }

            Page.Response.Redirect(SiteUrls.Instance().GetRecommendedUserNumberListUrl());
        }

        #region IReplyConverter<RecommendedUserNumberReply,GetOperation> Members

        public void ConvertReply(RecommendedUserNumberReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                recommendedUserNumber = reply.Entity as RecommendedUserNumber;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}